#include "DoOpControl.h"
#include "DevilSemantic.h"

void DoOpControl::ds_exec::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpControl::ds_if::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	
	if(toBoolean(obj2)->getBool()) {
#ifdef LANG_DEBUG
		state.outputDebugString(" ");
#endif
	
		obj1->execute(state);
		
#ifdef LANG_DEBUG
		state.outputDebugString(" ");
#endif
	}
}

void DoOpControl::ds_ifelse::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj3 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;

#ifdef LANG_DEBUG
	state.outputDebugString(" ");
#endif

	if (toBoolean(obj3)->getBool()) {
		obj2->execute(state);
	} else {
		obj1->execute(state);
	}
	
#ifdef LANG_DEBUG
	state.outputDebugString(" ");
#endif
}

void DoOpControl::ds_for::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj3 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj4 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj5;
			
#ifdef LANG_DEBUG
	state.outputDebugString(" ");
#endif

	int init = toNumber(obj4)->getInt();
	int inc = toNumber(obj3)->getInt();
	int limit = toNumber(obj2)->getInt();
	for(int i = init; i < limit; i += inc) {
		state.getOpStack().push(CRefPtr< _DoObject >(new DoNumber(i)));
		obj1->execute(state);
	}
	
#ifdef LANG_DEBUG
	state.outputDebugString(" ");
#endif
}

void DoOpControl::ds_repeat::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	
	int count = toNumber(obj2)->getInt();
	DoProcedure *proc = toProc(obj1);
	for(int i = 0; i < count; i++) {
		proc->execute(state);
	}
}

void DoOpControl::ds_loop::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpControl::ds_exit::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpControl::ds_stop::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpControl::ds_stopped::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	
	obj1->execute(state);
	state.getOpStack().push(newBool(false));
}

void DoOpControl::ds_countexecstack::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpControl::ds_quit::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpControl::ds_start::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

